<template>
  <div class="app-container">
    <div class="header flex">
      <FormSearch
        :formFields="searchList"
        @search="Search"
        @reset="Search"
      ></FormSearch>
    </div>
    <div class="footer">
      <div class="flex justify-end mb-3">
        <a-button type="primary" @click="handleAdd">新增标签</a-button>
      </div>
      <div class="table-inner">
        <a-table
          :columns="columns"
          :data-source="state.data"
          style="width: 100%"
          rowKey="label_id"
          :pagination="state.pagination"
          @change="pageChange"
          :loading="state.loading"
          :scroll="{ x: 1200 }"
        >
          <template #bodyCell="{ record, column }">
            <template v-if="column.dataIndex === 'status'">
              <a-switch
                v-model:checked="record.status"
                checked-children="开启"
                un-checked-children="关闭"
                :checkedValue="1"
                :unCheckedValue="0"
                @change="changeSwitch(record)"
              />
            </template>
            <template v-if="column.dataIndex === 'actions'">
              <a-space size="small">
                <a-button type="link" @click="handleEdit(record)"
                  >编辑</a-button
                >
                <a-popconfirm
                  title="你确定要删除吗?"
                  ok-text="确认"
                  cancel-text="取消"
                  @confirm="handleDelete(record)"
                >
                  <a-button type="link" danger>删除</a-button>
                </a-popconfirm>
              </a-space>
            </template>
          </template>
        </a-table>
      </div>
    </div>
  </div>
  <!--  弹出框-->
  <create-edit ref="createRef" @model-success="refresh" />
</template>
    
<script setup>
import { reactive, ref } from "vue";
import { list, del, open } from "@/api/goods/label";
import CreateEdit from "./components/CreateEdit.vue";
import { message } from "ant-design-vue";

const columns = [
  {
    title: "id",
    dataIndex: "label_id",
    width: 100,
  },
  {
    title: "名称",
    dataIndex: "name",
    width: 200,
  },
  {
    title: "背景颜色",
    dataIndex: "bg_color",
    width: 150,
  },
  {
    title: "字体颜色",
    dataIndex: "font_color",
    width: 150,
  },
  {
    title: "类型",
    dataIndex: "label_type",
    width: 180,
    customRender(record) {
      let cateType = ["", "空心", "实心"];
      return cateType[record.value];
    },
  },
  {
    title: "排序",
    dataIndex: "sort",
    width: 180,
  },
  {
    title: "状态",
    dataIndex: "status",
    width: 100,
  },
  {
    title: "创建时间",
    dataIndex: "create_time",
    width: 200,
  },
  {
    title: "操作",
    dataIndex: "actions",
    fixed: "right",
    width: 180,
  },
];
//搜索框数据
const searchList = reactive([
  {
    type: "input", //类型
    label: "名称", //标签
    name: "name", //字段
    value: "",
    placeholder: "输入名称",
  },
]);
const createRef = ref();
const state = reactive({
  data: [], //表格的数据
  loading: false,
  pagination: {
    total: 0,
    showSizeChanger: true, //是否页码可以改变
    current: 1, //当前xx页面
    pageSize: 10, //一页展示xx条数据
    showTotal: function (total) {
      return "总共" + total + "条数据";
    }, //总共xx条
    showQuickJumper: true,
  }, //分页数据
  searchParams: {
    page: 1,
    limit: 10,
  }, //搜索数据
});

//分页变动数据
const pageChange = (pagination) => {
  state.pagination.current = pagination.current;
  state.pagination.pageSize = pagination.pageSize;
  //搜索的参数
  state.searchParams.page = pagination.current;
  state.searchParams.limit = pagination.pageSize;
  getList();
};

//获取列表的数据
const getList = () => {
  state.loading = true;
  list({
    ...state.searchParams,
  }).then(function (res) {
    setTimeout(function () {
      state.loading = false;
    }, 500);
    if (res.code == 0) {
      state.loading = false;
      const data = res.data;
      state.data = data.data;
      state.pagination.total = data.total;
    }
  });
};

//搜索事件
const Search = (values) => {
  state.searchParams.page = 1;
  state.searchParams = {
    ...state.searchParams,
    ...values,
  };
  //搜索
  getList();
};
const refresh = () => {
  getList();
};
const changeSwitch = (record) => {
  open({
    id: record.label_id,
  }).then((res) => {
    if (res.code == 0) {
      message.success(res.msg);
      getList();
    }
  });
};
const handleAdd = () => {
  createRef.value.show();
};
const handleEdit = (record) => {
  createRef.value.show(record);
};
// 删除
const handleDelete = (record) => {
  del({ id: record.label_id }).then(function (res) {
    if (res.code == 0) {
      message.success(res.msg);
      getList();
    }
  });
};
//获取列表
getList();
</script>
    
<style scoped lang="scss">
@import "./index";
</style>
    